package tv.youxing.android.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import tv.youxing.android.R;
import tv.youxing.android.entity.Subscription_Booked;
import tv.youxing.android.entity.VideoPlayerPostModel;
import tv.youxing.android.ui.DynamicDetailActivity;
import tv.youxing.android.ui.SubscribeDeteilsActivity;
import tv.youxing.android.utils.PicassoUtils;

/**
 * Created by Administrator on 2017/6/19.
 */

public class RecommendAdapter extends BaseAdapter {
    private Context context;
    private List<Subscription_Booked.ResultBean.DataBean> orderedModelList = new ArrayList<>();
    private LayoutInflater layoutInflater;

    public RecommendAdapter(Context context, List<Subscription_Booked.ResultBean.DataBean> orderedModelList) {
        this.context = context;
        this.orderedModelList = orderedModelList;
        layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {

        return orderedModelList.size();
    }

    @Override
    public Object getItem(int position) {
        return orderedModelList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (viewHolder==null) {
            viewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.layout_item_ordered, null);
            viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
            viewHolder.ivHead = (ImageView) convertView.findViewById(R.id.iv_head);
            viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
            viewHolder.tvCateory = (TextView) convertView.findViewById(R.id.tvcategory);
            viewHolder.tvTime = (TextView) convertView.findViewById(R.id.tvTime);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }

        PicassoUtils.loadImageView(context,orderedModelList.get(position).getCover(),viewHolder.ivIcon);
        viewHolder.tvTitle.setText(orderedModelList.get(position).getAlbum_name());
        PicassoUtils.loadImageView(context,orderedModelList.get(position).getAlbum().getCover(),viewHolder.ivHead);

            viewHolder.tvCateory.setText("视频");

        viewHolder.ivIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent intent = new Intent(context, SubscribeDeteilsActivity.class);
//                intent.putExtra("multimedia_id",orderedModelList.get(position).getCont_id()+"");
//                context.startActivity(intent);

                Subscription_Booked.ResultBean.DataBean m = orderedModelList.get(position);
                VideoPlayerPostModel model = new VideoPlayerPostModel();
                model.setMultimedia_id(m.getMultimedia_id());
                model.setTitle(m.getTitle());
                model.setIntro(m.getIntro());
                model.setCover(m.getCover());
                model.setDuration(m.getDuration());
                model.setPublish_at(m.getPublish_at());
                model.setPlay_url(m.getPlay_url());
                model.setType(m.getType());
                model.setPlay_count(m.getPlay_count());
                model.setComment_count(m.getComment_count());
                model.setLike_count(m.getLike_count());
                model.setCollection_count(m.getCollection_count());
                model.setStatus(m.getStatus());
                model.setCreated_at(m.getCreated_at());
                model.setUpdated_at(m.getUpdated_at());
                model.setAlbum_name(m.getAlbum_name());
                model.setIs_collectied(m.getIs_collectied());
                model.setIs_like(m.getIs_like());
                model.setVideo_share_url(m.getVideo_share_url());
                VideoPlayerPostModel.AlbumBean albumBean = new VideoPlayerPostModel.AlbumBean();
                albumBean.setAlbum_id(m.getAlbum().getAlbum_id());
                albumBean.setName(m.getAlbum().getName());
                albumBean.setCover(m.getAlbum().getCover());
                albumBean.setIs_order(m.getAlbum().getIs_order());
                model.setAlbum(albumBean);

                Intent intent = new Intent(context, SubscribeDeteilsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("data",model);
                intent.putExtras(bundle);
                context.startActivity(intent);

            }
        });

        viewHolder.ivHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(context, DynamicDetailActivity.class);
                intent.putExtra("album_id",orderedModelList.get(position).getAlbum().getAlbum_id()+"");
                context.startActivity(intent);
            }
        });

        viewHolder.tvTime.setText(orderedModelList.get(position).getDuration());


        return convertView;
    }


    class ViewHolder{
        private ImageView ivIcon;
        private ImageView ivHead;
        private TextView tvTitle;
        private TextView tvCateory;
        private TextView tvTime;
    }
}
